package com.sgb.kjwl.viewmodel.enterprise;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.swgk.core.base.model.entity.ContactsEntitiy;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.util.UserOperating;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.SharedPreferenceUtil;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class ConTactsViewModel extends AndroidViewModel {

    public MutableLiveData<List<ContactsEntitiy>> listdata = new MutableLiveData<>();
    public MutableLiveData<String> delMsg = new MutableLiveData<>();
    public MutableLiveData<String> ToastShow = new MutableLiveData<>();
    //1show,2dismiss
    public MutableLiveData<Integer> showloading = new MutableLiveData<>();
    public boolean request = false;
    public boolean loadmore = false;
    private int page = 1;

    public ConTactsViewModel(@NonNull @NotNull Application application) {
        super(application);
    }


    public void getListdata(boolean loadmore) {
        if (loadmore) {
            page++;
        } else {
            page = 1;
        }

        showloading.postValue(1);
        NewAPIManager.getInstance().getEnterPrisenewAPI().getContaactList(
                page,
                10,
                UserOperating.getInstance().isCompany() ? 1 : UserOperating.getInstance().isTeam() ? 2 : 0,
                SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.USER_NO, ""),
                SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.COMPANY_NO, ""),
                SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.COMPANY_NO, "")).enqueue(new Callback<BaseEntity<ListResponse<List<ContactsEntitiy>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<ContactsEntitiy>>>> call, Response<BaseEntity<ListResponse<List<ContactsEntitiy>>>> response) {
                showloading.postValue(2);
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData().getList() != null) {
                        listdata.postValue(response.body().getData().getList());
                    }
                } else {
                    ToastShow.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<ContactsEntitiy>>>> call, Throwable t) {
                showloading.postValue(2);
                ToastShow.postValue(t.getMessage());
            }
        });

    }

    public void delcontact(String id) {
        showloading.postValue(1);
        NewAPIManager.getInstance().getEnterPrisenewAPI().delcontact("v1/app/enterpriseServices/order/"+id).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                showloading.postValue(2);
                if (response.body() == null ) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    delMsg.postValue(response.body().getMsg());
                } else {
                    ToastShow.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                showloading.postValue(2);
                ToastShow.postValue(t.getMessage());
            }
        });

    }
}
